# This is a sample Python script.

# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
from flask import Flask,render_template,request,redirect
app=Flask(__name__)
# 首页
@app.route("/")
def index():
    return render_template("index.html")
# 处理用户登录的请求
@app.route("/login",methods=['POST','GET'])
def login():
    username=request.form['username']
    userpwd = request.form['userpwd']
    if username == 'admin':
        if userpwd == 'admin123':
            return redirect("scrawl")
        else:
            return redirect("/")
    else:
        return redirect("/")
    return "ok"

@app.route("/scrawl")
def scrawler():
    return render_template("scrawl.html")
@app.route("/start",methods=['POST','GET'])
def start():
    name=request.form['targetname']
    url = request.form['targeturl']
    return str(crawl(url,selector="p"))

import requests
from bs4 import BeautifulSoup
def crawl(url, selector):
    headers={
        'user-agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36'
    }
    htmlcodes=requests.get(url,headers=headers).text
    bs=BeautifulSoup(htmlcodes,"html.parser")
    return [item.text for item in bs.select(selector=selector)]
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    app.run()
# See PyCharm help at https://www.jetbrains.com/help/pycharm/